<template>
  <a-modal
    class="loading"
    :visible="setModalVisible"
    width="25%"
    title="修改密码"
    @cancel="handleClose"
    @ok="handlePwdOk"
  >
    <a-form class="form" width="100%" ref="pwdForm" :model="formStore" :rules="rules">
      <a-form-item label="设置密码" name="pwd">
        <a-input v-model:value="formStore.pwd" width="100%" placeholder="请输入密码" />
      </a-form-item>
      <a-form-item label="重复密码" name="pwdTwo">
        <a-input v-model:value="formStore.pwdTwo" width="100%" placeholder="请输入密码" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import { updatePwdTeacher } from '/@/api/system-manage/teacher-info'
const formStore = ref<any>({})
const rules = {
  pwd: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
  pwdTwo: [
    {
      required: true,
      validator: async (_, value) => {
        if (value !== formStore.value.pwd) {
          return Promise.reject('两次密码不一致')
        }
      },
      trigger: 'blur',
    },
  ],
}

const props = defineProps({
  setModalVisible: {
    type: Boolean,
    default: false,
  },
  account: {
    type: String,
    default: '',
  },
})

const emit = defineEmits(['update:setModalVisible', 'update:account'])

function handleClose() {
  pwdForm.value?.resetFields()
  emit('update:setModalVisible', false)
  emit('update:account', '')
}

const pwdForm = ref<any>()
function handlePwdOk() {
  pwdForm.value?.validateFields().then(async values => {
    updatePwdTeacher({
      ...values,
      account: props.account,
    }).then(res => {
      if (res.code == 200) {
        message.success('操作成功')
        handleClose()
      } else {
        message.error(res.message)
      }
    })
  })
}
</script>
